//
//  RootViewController.m
//  UI_豆瓣更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
#import "RootTableViewCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrData;

@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x.png"] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
}

- (void)dealloc {
    [_tableView release];
    [_arrData release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"活动";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [self.tableView registerClass:[RootTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self dataHandle];
    
    
    
}



#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ActivityModel *model = _arrData[indexPath.row];
    cell.label1.text = model.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    NSString *aStr = [model.begin_time substringWithRange:NSMakeRange(5, 11)];
    NSString *bStr = [model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *cStr = [NSString stringWithFormat:@"%@ -- %@", aStr, bStr];
    cell.timeLabel.text = cStr;
    cell.addressLabel.text = model.address;
    cell.label3.text = model.category_name;
    NSString *str1 = [NSString stringWithFormat:@"%@", model.wisher_count];
    cell.label5.text = str1;
    NSString *str2 = [NSString stringWithFormat:@"%@", model.participant_count];
    cell.label7.text = str2;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 230;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVc = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVc animated:YES];
    ActivityModel *model = _arrData[indexPath.row];
    detailVc.str = model.title;
    detailVc.detailModel = model;
    detailVc.indexPath = indexPath;
    
}

/** 数据处理 */
- (void)dataHandle {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *tempArr = result[@"events"];
    self.arrData = [NSMutableArray array];
    for (NSDictionary *dic in tempArr) {
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arrData addObject:model];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
